import numpy as np

if __name__ == '__main__':
    matrix_random = np.arange(0, 9).reshape((3, 3))
    print(matrix_random)

    matrix_triangle_lower = np.tril(matrix_random)
    print(matrix_triangle_lower)

    # k = 0 为默认值，保留主对角线
    matrix_triangle_lower_1 = np.tril(matrix_random, k=0)
    print(matrix_triangle_lower_1)

    # k > 0 的时候，保留主对角线上面的个数
    matrix_triangle_lower_2 = np.tril(matrix_random, k=1)
    print(matrix_triangle_lower_2)

    matrix_triangle_lower_3 = np.tril(matrix_random, k=2)
    print(matrix_triangle_lower_3)

    # k < 0 的时候，删除从主对角线开始的个数
    matrix_triangle_lower_4 = np.tril(matrix_random, k=-1)
    print(matrix_triangle_lower_4)

    matrix_triangle_lower_5 = np.tril(matrix_random, k=-2)
    print(matrix_triangle_lower_5)
